Dispose() method permanently removes connection object from memory and the resource no longer exists for any further processing.
string constring = "Server=(local);Database=my; User Id=sa; Password=sa";
SqlConnection sqlcon = new SqlConnection(constring);
sqlcon.Open(); // connection is open
try
{ // code here which will be execute
}
catch
{ // code will be execute when error occurred in try block
}
finally
{
sqlcon.Dispose(); // close and desroy the connection object
}
Liked By
Write Answer
how use the dispose method for close the connection
Join MindStick Community
You have need login or register for voting of answers or question.
Shankar M
25-Feb-2013Shankar M
25-Feb-2013Shankar M
25-Feb-2013yash Singh
18-Dec-2010Dispose() method permanently removes connection object from memory and the resource no longer exists for any further processing.
string constring = "Server=(local);Database=my; User Id=sa; Password=sa";
SqlConnection sqlcon = new SqlConnection(constring);
sqlcon.Open(); // connection is open
try
{
// code here which will be execute
}
catch
{
// code will be execute when error occurred in try block
}
finally
{
sqlcon.Dispose(); // close and desroy the connection object
}